package com.rwx.reflect.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class Proxy01 {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
//        boolean add = list.add("123");
//        System.out.println(add);
//        System.out.println(list);
        List proxyList= (List) Proxy.newProxyInstance(Proxy01.class.getClassLoader(),
                new Class[]{List.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                        if(method.getName().equals("add")){
                            long startTime=System.currentTimeMillis();
                            method.invoke(list,args);

                            long endTime=System.currentTimeMillis();
                            System.out.println("耗时时间：" + (endTime - startTime));

                            return true;

                        }else if (method.getName().equals("remove")&&args[0] instanceof Integer){
                            System.out.println("拦截了按照索引删除的方法");
                            return null;
                        }else if (method.getName().equals("remove")){
                            System.out.println("拦截了按照对象删除的方法");
                            return false;
                        }else{
                            return method.invoke(list,args);
                        }



                    }
                });


        proxyList.add("aaa");
        proxyList.add("bbb");
        proxyList.add("ccc");

        proxyList.remove(0);
        proxyList.remove("aaa");


        System.out.println(list);

    }
}
